function add_fileextensions(seldir, appflag, extender, show)
%
% Routine to Append or PREPEND a file identifier to each filename within a directory.
% Simple Rule: 
%    if appflag is 0, the prepended part of the name will be the extender (!).
%    if appflag is 1, the appended part of the name will be the name of the
%    extender.
% 5/23/03 P. Manis.
% Done to allow sorting of data according to the rig on which the data was
% collected.
%
% Example command line:
% add_fileextensions('z:\data\incoming\cortex\rig3\*.mat', 1, '_r3', 1)
% arg 1 is the path and filename
% arg 2 is the appflag
% arg 3 is the append argument
% arg 4 is the "show' flag: if 1, show, if 0, DO it.
%

clear flist;

path0 = fileparts(seldir);

flist = fuf({seldir}, 2, 'detail');
flist=unique(flist); % just get the unique ones.
%flist
for i=1:length(flist)
    [path, fn, ext, ver]=fileparts(char(flist{i}));
        if(appflag == 0)
            fn = [extender fn(1:8)];
        else
            fn = [fn(1:8) extender];
        end;
        pn = fullfile(path, [fn ext]);
        fprintf(1, 'rename would be: %s \n', char(flist(i)));
        fprintf(1, '             to: %s \n\n', char(pn));
        cmd = sprintf('! move %s %s', char(flist(i)), char(pn));
        eval(cmd);
    end;
end;



